/**
 * Created by apple on 2017/9/30.
 */
var DBFactory = require('../db/mysql_db');
var UserSecDao = module.exports;
var Config = require('../config');


UserSecDao.checkUserSec = function (data,cb) {

    if (!data) return cb(new Error(500));

    DBFactory.poolQuery('SELECT * FROM user_app_module_security WHERE uid = ? and app_sid = ? and module = ? limit 1', [data.uid,data.app_sid,data.module],
        function () {
            cb('no collection');
        },
        function (err, result) {
            if (err) {
                console.log(err);
                cb(err);
                return;
            }
            if(result && result[0]){
                if(data.isLogin){
                    if(result[0].password == data.pwd){
                        cb(null,'登陆成功');
                    }else {
                        cb('密码错误');
                    }
                }else {
                    cb(null,'有设置密码锁');
                }
            }else {
                cb('没有设密码锁');
            }
        }
    );
}


UserSecDao.setMngPwd = function (data,cb) {
    if (!data) return cb(new Error(500));
    var sql = 'replace into user_app_module_security set ?';
    var param = {
        uid:data.uid,
        app_sid:data.app_sid,
        module:data.module,
        password:data.pwd
    };
    var value = [param];
    DBFactory.poolQuery(sql, value,
        function () {
            cb('no collection');
        },
        function (err, result) {
            if (err) {
                console.log(err);
                cb(err);
                return;
            }
            if(result.affectedRows > 0){
                cb(null,'操作成功');
            }
        }
    );
}

UserSecDao.delMngPwd = function (data,cb) {
    if (!data) return cb(new Error(500));
    var sql = 'delete from user_app_module_security where uid = ? and app_sid = ? and module = ?';
    var value = [data.uid,data.app_sid,data.module];
    DBFactory.poolQuery(sql, value,
        function () {
            cb('no collection');
        },
        function (err, result) {
            if (err) {
                console.log(err);
                cb(err);
                return;
            }
            if(result.affectedRows > 0){
                cb(null,'操作成功');
            }
        }
    );
}

